using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Graphics;
using GameStateManagementSample.QuickMath;
namespace GameStateManagementSample.Screens
{
    class Screen_Menu : GameScreen
    {
        #region Fields
        ContentManager m_Content;
        InputAction pauseAction;
        CircleMenu m_Circle;
        Texture2D m_TexBack;
        SpriteFont m_Font;
        #endregion

        #region Initialization
        public Screen_Menu()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.0f);
            TransitionOffTime = TimeSpan.FromSeconds(1.0f);
            pauseAction = new InputAction(
                new Buttons[] { Buttons.Start, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
            
        }
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (m_Content == null)
                {
                    m_Content = new ContentManager(ScreenManager.Game.Services, "Content");
                    ScreenManager.Game.ResetElapsedTime();
                    m_Circle = new CircleMenu(600, 280, 170, ScreenManager);

                    m_TexBack = m_Content.Load<Texture2D>(@"Image/mainmenu");
                    m_Font = m_Content.Load<SpriteFont>("gamefont");                   

                }
            }
        }
        public override void Deactivate()
        {
            base.Deactivate();
        }
        public override void Unload()
        {
            base.Unload();
        }
        #endregion
        #region Update and Draw
        protected virtual void OnCancel()
        {

        }
        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (!otherScreenHasFocus)
            {

            }
            m_Circle.Update(gameTime);
            UpdateTran();
        }
        public override void HandleInput(Microsoft.Xna.Framework.GameTime gameTime, InputState input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            PlayerIndex player;
            if (pauseAction.Evaluate(input, ControllingPlayer, out player))
            {
                /*foreach (GameScreen screen in ScreenManager.GetScreens())
                {
                    screen.ExitScreen();
                }
                ScreenManager.AddScreen(new PlayScreen(), null);*/
            }
            TouchCollection touches = input.TouchState;

            foreach (TouchLocation touch in touches)
            {
                if (touch.State == TouchLocationState.Pressed)
                {
                    Vector2 dis = touch.Position - m_Circle.Center;
                    if (dis.Length() < 49) TouchButtonOK();
                }
            }
            m_Circle.InputTouch(touches);
          
        }

       
        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            string des = "";
            switch (m_Circle.Select())
            {
                case 0:// Exit
                    des = "Exit game";
                    break;
                case 1:// Intro
                    des = "Introduction";
                    break;
                case 2:// Play
                    des = "Play now";
                    break;
                case 3:// About
                    des = "About game";
                    break;
            }
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            float pos = (TransitionPosition) * -800.0f;
            spriteBatch.Begin();
            spriteBatch.Draw(m_TexBack, Vector2.Zero + new Vector2(pos,0), Color.White);
            spriteBatch.DrawString(m_Font, des, new Vector2(170, 90)+new Vector2(pos,0), Color.White);
            spriteBatch.End();
            m_Circle.Draw();

        }
        public void UpdateTran()
        {
            if (TransitionPosition!=0)
            {
                m_Circle.Angle = TransitionPosition * (float)Math.PI *3;
            }
        }
        private void TouchButtonOK()
        {
            if (!m_Circle.isRotation)
            {
                foreach(GameScreen screen in ScreenManager.GetScreens())
                {
                    screen.ExitScreen();
                }
                switch (m_Circle.Select())
                {
                    case 0:// Exit
                        ScreenManager.Game.Exit();
                        break;
                    case 1:// Intro
                        ScreenManager.AddScreen(new Screen_Intro(), null);
                        break;
                    case 2:// Play
                        ScreenManager.AddScreen(new Screen_ChoseLevel(), null);
                        break;
                    case 3:// About
                        ScreenManager.AddScreen(new Screen_About(), null);
                        break;
                }
            }
        }
    }
        #endregion
     
}
